<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%-- 
    Document   : search
    Created on : Jul 4, 2012, 11:49:49 AM
    Author     : Ben
--%>

<%@page import="login.User"
        import="java.util.HashMap"
        import="java.lang.String"
        import="application.Seed"%>
<%@ include file="WEB-INF/jspf/header.jspf" %>
<% User currentUser = (User) session.getAttribute("currentSessionUser"); %>
<h3>Search</h3>
<div class="formDiv">
    <div class="searchDiv">
        <form name="searchForm" action="SearchServlet" method="POST">
            <span>Scientific Name: </span>
            <input name="scientificName" type="text"></input>
            <span> Hardiness Zone: </span>
            <select name="hardiness">
                <option value="blank"></option>
                <option value="0A">0A</option>
                <option value="0B">0B</option>
                <option value="1A">1A</option>
                <option value="1B">1B</option>
                <option value="2A">2A</option>
                <option value="2B">2B</option>
                <option value="3A">3A</option>
                <option value="3B">3B</option>
                <option value="4A">4A</option>
                <option value="4B">4B</option>
                <option value="5A">5A</option>
                <option value="5B">5B</option>
                <option value="6A">6A</option>
                <option value="6B">6B</option>
                <option value="7A">7A</option>
                <option value="7B">7B</option>
                <option value="8A">8A</option>
                <option value="8B">8B</option>
                <option value="9A">9A</option>
                <option value="9B">9B</option>
                <option value="10A">10A</option>
                <option value="10B">10B</option>
                <option value="11A">11A</option>
                <option value="11B">11B</option>
                <option value="12A">12A</option>
                <option value="12B">12B</option>
            </select>
            <input type="submit" value="submit"></input>
        </form>
        <% 
        if(!(request.getParameter("hardiness")==null && request.getParameter("scientificName")==null)){
            String hardiness=null;
            String scientificName = null;
            if(request.getParameter("hardiness")!=null)
                hardiness = request.getParameter("hardiness");
            if(request.getParameter("scientificName")!=null)
                scientificName = request.getParameter("scientificName");
        %>
        <sql:query var="hardinessAndName" dataSource="jdbc/CS2340SeedSwap">
            SELECT * FROM seeds WHERE 
            <%if(hardiness!=null){%>
            hardiness="<%=hardiness%>"<%}%>
            <%if(hardiness!=null && scientificName!=null){%> AND <%}%>
            <%if(scientificName!=null){%>
            scientificName='<%=scientificName.replaceAll("'","''")%>'
            <%}%>
        </sql:query>
        <table border="1">
            <!-- column headers -->
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Scientific Name</th>
                <th>Growing Tips</th>
                <th>Special Requirements</th>
                <th>Harvesting Information</th>
                <th>Growing Season</th>
                <th>Climate Zone</th>
                <th>Hardiness</th>
                <th>Seller's Username</th>
                <th>Quantity (kg.)</th>
                <th>Price</th>
            </tr>
            <!-- column data -->
            <c:forEach var="row" items="${hardinessAndName.rowsByIndex}">
                <tr>
                <c:forEach var="column" items="${row}">
                    <td><c:out value="${column}"/></td>
                </c:forEach>
                </tr>
            </c:forEach>
        </table>
        <%
        }
        %>
    </div>
    </div>
<%@ include file="WEB-INF/jspf/footer.jspf" %>